from ..base import PermissionRuleHandler, register_handler

@register_handler("field_in_list")  
class FieldInListHandler(PermissionRuleHandler):
    def get_required_config_keys(self):
        return ["user_list_field", "target_field"]

    def validate_config(self, config):
        return all(k in config for k in self.get_required_config_keys())

    def generate_sql(self, table_alias: str, config: dict, user: dict) -> str:
        ids = user.get(config["user_list_field"], [])
        if not ids:
            return "1=0"
        # 正确处理字符串值的引用
        quoted_ids = []
        for x in ids:
            if isinstance(x, str):
                quoted_ids.append(f"'{x}'")
            else:
                quoted_ids.append(str(x))
        ids_str = ",".join(quoted_ids)
        return f"{table_alias}.{config['target_field']} IN ({ids_str})"